package net.ib.mtalk.network.task;

import java.util.List;

import android.content.Context;
import android.os.Handler;
import android.util.Log;
import net.ib.mntalk.protocol.Common;
import net.ib.mntalk.protocol.Common.BuddyChange;
import net.ib.mntalk.protocol.Common.UUIDMsg;
import net.ib.mntalk.protocol.Common.UUIDSlicePredicate;
import net.ib.mntalk.protocol.Common.UUIDSliceRange;
import net.ib.mntalk.protocol.Session.AddContactsRes;
import net.ib.mntalk.protocol.Session.GetBuddyChangesReq;
import net.ib.mntalk.protocol.Session.GetBuddyChangesRes;
import net.ib.mntalk.protocol.Session.GetBuddyInfosReq;
import net.ib.mtalk.constant.HandlerMsgConst;
import net.ib.mtalk.database.helper.BuddyDatabaseHelper;
import net.ib.mtalk.network.MethodId.Session;
import net.ib.mtalk.network.TcpEnvelope;
import net.ib.mtalk.ui.buddy.talk.BuddyTalkListActivity;
import net.ib.mtalk.util.GpbSupport;
import net.ib.mtalk.util.MTalkLogWriter;
import net.ib.mtalk.util.NotifyingAsyncQueryHandler;

public class GetBuddyChangesTask extends AsyncNetTask {
	
	private GetBuddyChangesReq mGetBuddyChangesReq;

	public GetBuddyChangesTask(Context _context, Handler _uiHandler, GetBuddyChangesReq getBuddyChangesReq) {
		super(_context, _uiHandler);
		this.mGetBuddyChangesReq = getBuddyChangesReq;
	}

	@Override
	protected TcpEnvelope onPreExecute() {
		TcpEnvelope rev = new TcpEnvelope.Builder()
			.setType(TcpEnvelope.REQUEST)
			.setMessage(mGetBuddyChangesReq)
			.setMethodId(Session.Contact.GetBuddyChanges)
			.build();
		return rev;
	}

	@Override
	protected void onPostExecute(int resultCode, Object sentObject, Object receivedObject) {
		MTalkLogWriter.d("GetBuddyChangesTask.onPostExecute() resultCode : " + resultCode);
		if (receivedObject == null) {
			mUiHandler.sendMessageAtFrontOfQueue(mUiHandler.obtainMessage(HandlerMsgConst.HANDLER_UNKNOWN_ERROR));
			return;
		}
		
		TcpEnvelope resultEnvelope = (TcpEnvelope) receivedObject;
		GetBuddyChangesRes getBuddyChangesRes = (GetBuddyChangesRes) resultEnvelope.getMessage();
		
		MTalkLogWriter.d("GetBuddyChangesTask ResponseCode : " + getBuddyChangesRes.getResponseCode());
		
		if(getBuddyChangesRes.getResponseCode() == Common.SUCCESS){
			mUiHandler.sendMessageAtFrontOfQueue(mUiHandler.obtainMessage(HandlerMsgConst.HANDLER_GET_BUDDY_CHANGES_SUCCESS));
			GobuddyChange(getBuddyChangesRes);
		} else if(getBuddyChangesRes.getResponseCode() == Common.EXPIRED_CACHE){
			
		} else {
			mUiHandler.sendMessageAtFrontOfQueue(mUiHandler.obtainMessage(HandlerMsgConst.HANDLER_GET_BUDDY_CHANGES_FAIL));
		}
	}

	private void GobuddyChange(GetBuddyChangesRes getBuddyChangesRes) {
		List<BuddyChange> buddyChanges = getBuddyChangesRes.getBuddyChangeList();
		if(buddyChanges != null){
			for(BuddyChange change : buddyChanges){
				Log.i("ZOO", "change.getType() = " + change.getType());
				GetBuddyInfosReq getBuddyInfosReq = new GetBuddyInfosReq();
				UUIDSlicePredicate uuidSlicePredicate = null;
				switch (change.getType()) {
				case Common.ADDED:
//					getBuddyInfosReq.setPredicate(new Common.UUIDSlicePredicate().setSliceRange(new Common.UUIDSliceRange().setStart(change.getNotiUuid()).setAscending(true).setMaxCount(100)))
					uuidSlicePredicate = new Common.UUIDSlicePredicate().addId(change.getNotiUuid());
					break;
				case Common.REMOVED:
					
					break;
				case Common.MODIFIED:
					
					break;

				default:
					break;
				}
				getBuddyInfosReq.setPredicate(uuidSlicePredicate);
				getBuddyInfosReq.addProperty(Common.USER_PROPERTY_NAME).addProperty(Common.USER_PROPERTY_PROFILE_IMAGE).addProperty(Common.USER_PROPERTY_STATUS).addProperty(Common.USER_PROPERTY_BIRTHDAY);
				GetBuddyInfosTask getBuddyInfosTask = new GetBuddyInfosTask(mContext, mUiHandler, getBuddyInfosReq);
				getBuddyInfosTask.execute();
			}
		}
	}
}
